// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    cardCode: [],
    browse:[],
    shop:[],
    gives:'',
    isgive:1,
    indicatorDots: false,
    show: false,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },
  showPopup() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  toechars(){
    // 不能跳转到底部导航得页面
    wx.navigateTo({
      url:  "/pages/vr/index",
    })
  },
  changeIndicatorDots() {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay() {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange(e) {
    this.setData({
      duration: e.detail.value
    })
  },
  getCardPage(){
    // 获取名片页面数据
    var that = this
    wx.request({
      url: 'http://192.168.3.150:10001/wechat/touch/AddBrowsingRecord',
      data:{businessCardId:6,commodityId:1},
      method: 'POST',
      header: {
    		'content-type': 'application/json' // 默认值
  	  },
      success:function(res){
        console.log(res)
      },
      fail:function(){
        wx.showToast({
          icon: "none",
          mask: true,
          title: "接口调用失败，请稍后再试。",
       });
      }
    })
    wx.request({
      url: 'http://192.168.3.150:10001/wechat/touch/BusinessCardHomePageStatic',
      data:{BusinesCardId:15},
      method: 'POST',
      header: {
    		'content-type': 'application/json' // 默认值
  	  },
      success:function(res){
        console.log(res.data.data)
        that.setData({browse:res.data.data.browse}) 
        that.setData({userInfo:res.data.data.businessCard})  //名片信息
        that.setData({cardCode:res.data.data.businessCardsCode})   //名片码
        that.setData({shop:res.data.data.recommend})
        that.setData({gives:res.data.data.gives})
      },
      fail:function(){
        wx.showToast({
          icon: "none",
          mask: true,
          title: "接口调用失败，请稍后再试。",
       });
      }
    })
  },
  recommend(){
    // 点赞
    var that = this
    wx.request({
      url: 'http://192.168.3.100:10001/wechat/touch/ModifyLike',
      data:{businessCardId:6,commodityId:1},
      method: 'POST',
      header: {
    		'content-type': 'application/json' // 默认值
  	  },
      success:function(res){
        console.log(res)
        let give = ''
        if(that.data.isgive==1){
          give = ++that.data.gives
          that.setData({gives:give})
        }else if(that.data.isgive==2){
          give = --that.data.gives
          that.setData({gives:give})
        }
      },
      fail:function(){
        wx.showToast({
          icon: "none",
          mask: true,
          title: "接口调用失败，请稍后再试。",
       });
      }
    })
  },
  onLoad() {
    this.getCardPage()
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
